#include <vtkForceDirectedLayoutStrategy.h>
#include <vtkGraphLayoutView.h>
#include <vtkMutableUndirectedGraph.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

#include <vtkArcSource.h>
#include <vtkPolyDataMapper.h>
#include "vtkPolygon.h"
#include "vtkProperty.h"

#include "vtkLookupTable.h"
#include "vtkMinimalStandardRandomSequence.h"
#include "vtkCellData.h"
#include "vtkFloatArray.h"

#include "vtkGlyph3DMapper.h"
#include "vtkSphereSource.h"

#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkPolyDataMapper2D.h"
#include "vtkMapper2D.h"
#include "vtkActor2D.h"
#include "vtkCamera.h"
#include "vtkProperty2D.h"
#include "vtkProp3D.h"
#include "vtkExtractVOI.h"
#include "vtkPlaneSource.h"
#include "vtkPlane.h"
#include "vtkSampleFunction.h"
#include "vtkContourFilter.h"
#include "vtkAppendFilter.h"
#include "vtkDataSetMapper.h"
#include "vtkClipPolyData.h"
#include "vtkImplicitBoolean.h"
#include "vtkImplicitFunction.h"
#include "vtkDiskSource.h"
#include "vtkExtractPolyDataGeometry.h"
#include "vtkImplicitSum.h"
#include "vtkClipClosedSurface.h"
#include "vtkPlaneCollection.h"
#include "vtkCellPicker.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkColorTransferFunction.h"

#include "vector"
#include "array"


#include "vtkRegressionTestImage.h"
//#include "vtkTestUtilities.h"

#include "vtkActor.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"

#include "vtkCamera.h"
#include "vtkDataSetSurfaceFilter.h"
#include "vtkImageData.h"
#include "vtkImageDataGeometryFilter.h"
#include "vtkImageSinusoidSource.h"
#include "vtkLookupTable.h"
#include "vtkPolyDataMapper.h"

#include "vtkImageActor.h"
#include "vtkImageReader2Factory.h"
#include "vtkImageReader2.h"
#include "vtkImageMapper3D.h"
#include "vtkImageProperty.h"



int main(int, char*[]) {

//    vtkNew<vtkRenderer> renderer;
//    vtkNew<vtkRenderWindow> renderWindow;
//    renderWindow->SetWindowName("RegularPolygonSource");
//    renderWindow->AddRenderer(renderer);
//    vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
////    renderWindowInteractor->SetInteractorStyle(vtkNew<vtkInteractorStyleTrackballCamera>)
//    renderWindowInteractor->SetRenderWindow(renderWindow);
//
//
//    vtkNew<vtkNamedColors> colors;
//
//
//
//
//    renderWindow->Render();
//    renderWindowInteractor->Start();


    vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkRenderWindowInteractor::New();
    vtkSmartPointer<vtkRenderWindow> renWin = vtkRenderWindow::New();
    iren->SetRenderWindow(renWin);

    vtkSmartPointer<vtkRenderer> renderer = vtkRenderer::New();
    renWin->AddRenderer(renderer);


    vtkSmartPointer<vtkImageSinusoidSource> imageSource = vtkImageSinusoidSource::New();
    imageSource->SetWholeExtent(0, 9, 0, 9, 0, 9);
    imageSource->SetPeriod(5);
    imageSource->Update();

    vtkSmartPointer<vtkImageData> image = imageSource->GetOutput();
    double range[2];
    image->GetScalarRange(range);

    vtkSmartPointer<vtkDataSetSurfaceFilter> surface = vtkDataSetSurfaceFilter::New();
    surface->SetInputConnection(imageSource->GetOutputPort());

    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkPolyDataMapper::New();
    mapper->SetInputConnection(surface->GetOutputPort());
//    mapper->SetInputConnection(imageSource->GetOutputPort());

    vtkSmartPointer<vtkLookupTable> lut = vtkLookupTable::New();
    lut->SetTableRange(range);
    lut->SetAlphaRange(0.5, 0.5);
    lut->SetHueRange(0.2, 0.7);
    lut->SetNumberOfTableValues(256);
    lut->Build();

    mapper->SetScalarVisibility(1);
    mapper->SetLookupTable(lut);
    mapper->UseLookupTableScalarRangeOn();
    mapper->InterpolateScalarsBeforeMappingOn();

    vtkSmartPointer<vtkActor> actor = vtkActor::New();
    renderer->AddActor(actor);
    actor->SetMapper(mapper);

    renderer->SetBackground(0.1, 0.3, 0.0);
    renWin->SetSize(400, 400);
//    renWin->Render();

    vtkSmartPointer<vtkCamera> camera = renderer->GetActiveCamera();
    camera->Azimuth(-40.0);
    camera->Elevation(20.0);




    char *inputFilename = "/Users/dai/Work/vtk/VTK-Example/AMy/ImageMap/map3.png";

    vtkNew<vtkImageReader2Factory> readerFactory;
    vtkSmartPointer<vtkImageReader2> imageReader;
    imageReader.TakeReference(readerFactory->CreateImageReader2(inputFilename));
    imageReader->SetFileName(inputFilename);

    vtkNew<vtkImageActor> actor3;
    actor3->GetMapper()->SetInputConnection(imageReader->GetOutputPort());
    actor3->Update();
    double *imageBounds = actor3->GetBounds();
    std::cout << imageBounds[0] << "-" << imageBounds[1] << "-" << imageBounds[2] << "-" << imageBounds[3];
    double minW = 800 * 2;
    double imageW = imageBounds[1] - imageBounds[0];
    double imageH = imageBounds[3] - imageBounds[2];
    double scale = minW / imageW;
    double x = -imageW * scale * 0.5;
    double y = -imageH * scale * 0.5;
    actor3->SetPosition(x, y, -200);
    actor3->SetScale(scale);
    actor3->GetProperty()->BackingOn();
    actor3->GetProperty()->SetBackingColor(1, 0, 0);
    actor3->GetProperty()->SetColorLevel(0);
    renderer->AddActor(actor3);



    renWin->Render();
    iren->Start();

//    int retVal = vtkRegressionTestImage(renWin);
//    if (retVal == vtkRegressionTester::DO_INTERACTOR)
//    {
//        iren->Start();
//    }
//    iren->Delete();

    return EXIT_SUCCESS;
}